package basics;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class TestAll {

    /**
     *  StringBuffer 线程安全    --> append方法加了synchronized
     *  StringBuilder 线程不安全
     * @throws InterruptedException
     */
    @Test
    public void testStringThreadSafe() throws InterruptedException {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(1);
        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append(1);

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            System.out.println(finalI);
            new Thread(() -> {
                stringBuilder.append(finalI);
            }).start();
        }

        System.out.println(stringBuilder.toString());
        TimeUnit.SECONDS.sleep(5L);
    }
}
